package com.gxa.saas.portal.controller.luoj;

import com.gxa.saas.core.common.JsonResult;
import com.gxa.saas.core.entity.dto.luoj.ProductDTO;
import com.gxa.saas.core.entity.dto.luoj.ShoppingCart;
import com.gxa.saas.portal.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/cart")
@Api(value = "购物车")
public class CarController {

    @Resource
    private ShoppingCartService shoppingCartService;

    @ApiOperation("查看用户购物车")
    @GetMapping("/getAllCarById")
    public Map getAllCarById(Integer userId){
        ShoppingCart shoppingCart = shoppingCartService.getShoppingCart(userId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(shoppingCart);
        jsonResult.setMsg("查询成功！");
        return jsonResult.getMap();
    }

    @ApiOperation("编辑中的删除操作（批量删除）")
    @PostMapping("/delCarByBatch")
    public Map delCarByBatch(@RequestBody ProductDTO... productDTO){
        for (ProductDTO dto : productDTO) {
            shoppingCartService.delProductForShoppingCart(dto.getUserId(), dto.getId());
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");

        jsonResult.setMsg("删除成功！");

        return jsonResult.getMap();
    }

    @ApiOperation("单个删除")
    @PostMapping("/delCarByIds")
    public Map delCarById(@RequestBody ProductDTO productDTO){
        shoppingCartService.delProductForShoppingCart(productDTO.getUserId(), productDTO.getId());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功！");
        return jsonResult.getMap();
    }

    @ApiOperation("支付")
    @PostMapping("/payCar")
    public Map payCar(@RequestBody ShoppingCart shoppingCart){
        Boolean integer = shoppingCartService.buyShoppingCart(shoppingCart);
        JsonResult jsonResult = new JsonResult();
        if (integer!=false) {
            jsonResult.setCode("200");
            jsonResult.setMsg("支付成功！");
        }
        else
        {
            jsonResult.setCode("0");
            jsonResult.setMsg("支付失败！");
        }
        return jsonResult.getMap();
    }

    @ApiOperation("将商品添加到购物车")
    @PostMapping("/addProductForCart")
    public Map addProductForCart(@RequestBody ProductDTO productDTO)  {
        shoppingCartService.addProduct(productDTO);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功！");
        return jsonResult.getMap();
    }
}
